home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacScrollBarUI.java < prev    next >
Text File  |  1998-06-30  |  15KB  |  497 lines

  1. /*
  2.  * @(#)MacScrollBarUI.java    1.6 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.event.*;
  25. import com.sun.java.swing.plaf.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.plaf.basic.BasicScrollBarUI;
  28.  
  29. import java.awt.event.MouseListener;
  30. import java.awt.event.MouseEvent;
  31. import java.awt.event.WindowEvent;
  32.  
  33. import java.awt.Container;
  34. import java.awt.Window;
  35. import java.awt.Dimension;
  36. import java.awt.Insets;
  37. import java.awt.Rectangle;
  38. import java.awt.Graphics;
  39. import java.awt.Color;
  40.  
  41. /**
  42.  * Implementation of ScrollBarUI for the Mac Look and Feel
  43.  * <p>
  44.  * Warning: serialized objects of this class will not be compatible with
  45.  * future swing releases.  The current serialization support is appropriate
  46.  * for short term storage or RMI between Swing1.0 applications.  It will
  47.  * not be possible to load serialized Swing1.0 objects with future releases
  48.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  49.  * baseline for the serialized form of Swing objects.
  50.  *
  51.  * @version @(#)MacScrollBarUI.java    1.0 11/24/97
  52.  * @author Symantec
  53.  */
  54. public class MacScrollBarUI extends BasicScrollBarUI implements MouseListener
  55. {
  56.     private static boolean scrollBarColorsInitialized = false;
  57.     
  58.     private static Color scrollBarBackgroundEnabled;
  59.     private static Color scrollBarBackgroundDisabled;
  60.  
  61.     private static Color scrollBarBorderActive;
  62.     private static Color scrollBarBorderInactive;
  63.  
  64.     private static Color scrollBarBevelIn1;
  65.     private static Color scrollBarBevelIn2;
  66.     private static Color scrollBarBevelCorner;
  67.     private static Color scrollBarBevelOut2;
  68.     private static Color scrollBarBevelOut1;
  69.  
  70.     private static ImageIcon scrollBoxVerticalEnabled;
  71.     private static ImageIcon scrollBoxVerticalPressed;
  72.     private static ImageIcon scrollBoxHorizontalEnabled;
  73.     private static ImageIcon scrollBoxHorizontalPressed;
  74.  
  75.     private static Dimension scrollBoxVerticalSize;
  76.     private static Dimension scrollBoxHorizontalSize;
  77.  
  78.     private boolean drawScrollBoxPressed = false;
  79.     private ImageIcon scrollBoxEnabled;
  80.     private ImageIcon scrollBoxPressed;
  81.  
  82.     private Dimension scrollBoxSize = new Dimension(16, 16);    // Defaults
  83.     private Rectangle thumbBevel = new Rectangle(0, 0, 0, 0);
  84.     
  85.     protected ActivationHelper activationHelper;
  86.     protected ChangeListener changeListener;
  87.     protected boolean isActive = true;
  88.  
  89.     public static ComponentUI createUI(JComponent c)
  90.     {
  91.         return new MacScrollBarUI();
  92.     }
  93.     
  94.     public void installUI(JComponent c)
  95.     {
  96.         super.installUI(c);
  97.  
  98.         c.addMouseListener(this);
  99.         activationHelper    = new ActivationHelper(c);
  100.         changeListener        = new ChangeListener();
  101.         activationHelper.addPropertyChangeListener(changeListener);
  102.     }
  103.     
  104.     public void uninstallUI(JComponent c)
  105.     {
  106.         c.removeMouseListener(this);
  107.         activationHelper.removePropertyChangeListener(changeListener);
  108.         activationHelper = null;
  109.         changeListener = null;
  110.  
  111.         super.uninstallUI(c);
  112.     }
  113.     
  114.     public boolean isActive()
  115.     {
  116.         return isActive;
  117.     }
  118.  
  119.     protected void configureScrollBarColors()
  120.     {
  121.         if (!scrollBarColorsInitialized)
  122.         {
  123.             scrollBarBackgroundEnabled = UIManager.getColor("ScrollBar.backgroundEnabled");
  124.             scrollBarBackgroundDisabled = UIManager.getColor("ScrollBar.backgroundDisabled");
  125.  
  126.             scrollBarBorderActive = UIManager.getColor("ScrollBar.borderActive");
  127.             scrollBarBorderInactive = UIManager.getColor("ScrollBar.borderInactive");
  128.  
  129.             scrollBarBevelIn1 = UIManager.getColor("ScrollBar.bevelIn1");
  130.             scrollBarBevelIn2 = UIManager.getColor("ScrollBar.bevelIn2");
  131.             scrollBarBevelCorner = UIManager.getColor("ScrollBar.bevelCorner");
  132.             scrollBarBevelOut2 = UIManager.getColor("ScrollBar.bevelOut2");
  133.             scrollBarBevelOut1 = UIManager.getColor("ScrollBar.bevelOut1");
  134.  
  135.             scrollBoxVerticalEnabled = (ImageIcon) UIManager.getIcon("ScrollBar.verticalBoxEnabled");
  136.             scrollBoxVerticalPressed = (ImageIcon) UIManager.getIcon("ScrollBar.verticalBoxPressed");
  137.             scrollBoxHorizontalEnabled = (ImageIcon) UIManager.getIcon("ScrollBar.horizontalBoxEnabled");
  138.             scrollBoxHorizontalPressed = (ImageIcon) UIManager.getIcon("ScrollBar.horizontalBoxPressed");
  139.  
  140.             scrollBoxVerticalSize = new Dimension(scrollBoxVerticalEnabled.getIconWidth(), scrollBoxVerticalEnabled.getIconHeight());
  141.             scrollBoxHorizontalSize = new Dimension(scrollBoxHorizontalEnabled.getIconWidth(), scrollBoxHorizontalEnabled.getIconHeight());
  142.  
  143.             AppearanceFilter.FilterImageIcon(scrollbar, scrollBoxVerticalEnabled);
  144.             AppearanceFilter.FilterImageIcon(scrollbar, scrollBoxVerticalPressed);
  145.             AppearanceFilter.FilterImageIcon(scrollbar, scrollBoxHorizontalEnabled);
  146.             AppearanceFilter.FilterImageIcon(scrollbar, scrollBoxHorizontalPressed);
  147.  
  148.             scrollBarColorsInitialized = true;
  149.         }
  150.  
  151.         switch (scrollbar.getOrientation())
  152.         {
  153.             case JScrollBar.VERTICAL:
  154.                 scrollBoxEnabled = scrollBoxVerticalEnabled;
  155.                 scrollBoxPressed = scrollBoxVerticalPressed;
  156.                 scrollBoxSize.setSize(scrollBoxVerticalSize);
  157.                 break;
  158.             
  159.             case JScrollBar.HORIZONTAL:
  160.                 scrollBoxEnabled = scrollBoxHorizontalEnabled;
  161.                 scrollBoxPressed = scrollBoxHorizontalPressed;
  162.                 scrollBoxSize.setSize(scrollBoxHorizontalSize);
  163.                 break;
  164.         }
  165.     }
  166.     
  167.     public Dimension getPreferredSize(JComponent c)
  168.     {
  169.         Insets insets = c.getInsets();
  170.         int dx = insets.left + insets.right;
  171.         int dy = insets.top + insets.bottom;
  172.         return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
  173.             ? new Dimension(dx + 16, dy + 32)
  174.             : new Dimension(dx + 32, dy + 16);
  175.     }
  176.     
  177.     protected Dimension getMinimumThumbSize()
  178.     {
  179.         return scrollBoxSize;
  180.     }
  181.     
  182.     protected Dimension getMaximumThumbSize()
  183.     {
  184.         return scrollBoxSize;
  185.     }
  186.  
  187.     /**
  188.      * Set the bounds of the thumb and force a repaint that includes
  189.      * the old thumbBounds and the new one.
  190.      * Overridden to repaint the Thumb's bevel
  191.      *
  192.      * @see #getThumbBounds
  193.      */
  194.     protected void setThumbBounds(int x, int y, int width, int height)
  195.     {
  196.         Rectangle oldThumbBevel = new Rectangle(thumbBevel);
  197.  
  198.         super.setThumbBounds(x, y, width, height);
  199.  
  200.         if (!oldThumbBevel.isEmpty())
  201.             scrollbar.repaint(oldThumbBevel);        // Un paint the existing thumb Bevel
  202.  
  203.         /* Calculate the new thumb Bevel
  204.         */
  205.         if (scrollbar.getOrientation() == JScrollBar.VERTICAL)
  206.         {
  207.             y += height;
  208.             if (y + 2 > incrButton.getY())
  209.                 height = 0;
  210.             else
  211.                 height = 2;
  212.         }
  213.         else
  214.         {
  215.             x += width;
  216.             if (x + 2 > incrButton.getX())
  217.                 width = 0;
  218.             else
  219.                 width = 2;
  220.         }
  221.         thumbBevel.setBounds(x, y, width, height);
  222.  
  223.         if (!thumbBevel.isEmpty())
  224.             scrollbar.repaint(thumbBevel);        // Paint the new thumb Bevel
  225.     }
  226.     
  227.     protected JButton createDecreaseButton(int orientation)
  228.     {
  229.         return new ScrollBarButton(orientation);
  230.     } 
  231.     
  232.     protected JButton createIncreaseButton(int orientation)
  233.     {
  234.         return new ScrollBarButton(orientation);
  235.     }
  236.     
  237.     private void paintVerticalContentBevel(Graphics g, int x, int y, int width, int height)
  238.     {
  239.         g.translate(x, y);
  240.  
  241.         g.setColor(scrollBarBevelIn1);
  242.         g.drawLine(0, 0, width-1, 0);
  243.         g.drawLine(0, 1, 0, height);
  244.  
  245.         g.setColor(scrollBarBevelIn2);
  246.         g.drawLine(1, 1, width-2, 1);
  247.         g.drawLine(1, 2, 1, height);
  248.  
  249.         g.setColor(scrollBarBevelCorner);
  250.         g.drawLine(width, 0, width, 0);
  251.         g.drawLine(width-1, 1, width-1, 1);
  252.  
  253.         g.setColor(scrollBarBevelOut2);
  254.         g.drawLine(width-1, 2, width-1, height);
  255.         
  256.         g.setColor(scrollBarBevelOut1);
  257.         g.drawLine(width, 1, width, height);
  258.  
  259.         g.translate(-x, -y);
  260.     }
  261.  
  262.     private void paintHorizontalContentBevel(Graphics g, int x, int y, int width, int height)
  263.     {
  264.         g.translate(x, y);
  265.  
  266.         g.setColor(scrollBarBevelIn1);
  267.         g.drawLine(0, 0, width, 0);
  268.         g.drawLine(0, 1, 0, height-1);
  269.  
  270.         g.setColor(scrollBarBevelIn2);
  271.         g.drawLine(1, 1, width, 1);
  272.         g.drawLine(1, 2, 1, height-2);
  273.  
  274.         g.setColor(scrollBarBevelCorner);
  275.         g.drawLine(1, height-1, 1, height-1);
  276.         g.drawLine(0, height, 0, height);
  277.  
  278.         g.setColor(scrollBarBevelOut2);
  279.         g.drawLine(2, height, width, height);
  280.         
  281.         g.setColor(scrollBarBevelOut1);
  282.         g.drawLine(1, height, width, height);
  283.  
  284.         g.translate(-x, -y);
  285.     }
  286.  
  287.     protected void paintBackground(Graphics g, JComponent c)
  288.     {
  289.         g.setColor(c.getBackground());
  290.         g.fillRect(0,0,c.getWidth(),c.getHeight());
  291.  
  292.         // Determine the coordinates of the control area of the scroll bar.
  293.         Insets insets = scrollbar.getInsets();
  294.         int x = insets.left;
  295.         int y = insets.top;
  296.         int w = scrollbar.getWidth() - insets.right - x - 1;
  297.         int h = scrollbar.getHeight() - insets.bottom - y - 1;
  298.         
  299.         if (!isActive)
  300.         {
  301.             g.setColor(scrollBarBackgroundDisabled);
  302.             g.fillRect(x, y, w, h);
  303.  
  304.             g.setColor(scrollBarBorderInactive);
  305.             g.drawRect(x, y, w, h);
  306.         }
  307.         else
  308.         {
  309.             // Paint the border
  310.             g.setColor(scrollBarBorderActive);
  311.             g.drawRect(x, y, w, h);
  312.  
  313.             x += 1;
  314.             y += 1;
  315.             w -= 2;
  316.             h -= 2;
  317.  
  318.             if (scrollbar.isEnabled() && (scrollbar.getValue() >= 0) && (scrollbar.getMinimum() < scrollbar.getMaximum()))
  319.             {
  320.                 // First, fill the scrollbar with the background color
  321.                 g.setColor(scrollBarBackgroundEnabled);
  322.                 g.fillRect(x, y, w, h);
  323.  
  324.                 Rectangle thumbR = getThumbBounds();
  325.  
  326.                 // Draw Top & Bottom background area
  327.                 if (scrollbar.getOrientation() == JScrollBar.VERTICAL)
  328.                 {
  329.                     int topY = decrButton.getY() + decrButton.getHeight();
  330.                     int topHeight = thumbR.y - topY;
  331.                     if (topHeight > 0)
  332.                         paintVerticalContentBevel(g, x, topY, w, topHeight);
  333.  
  334.                     int botY = thumbR.y + thumbR.height;
  335.                     int botHeight = incrButton.getY() - botY;
  336.                     if (botHeight > 0)
  337.                         paintVerticalContentBevel(g, x, botY, w, botHeight);
  338.                 } 
  339.                 // Draw Left & Right background area
  340.                 else
  341.                 {
  342.                     int leftX = decrButton.getX() + decrButton.getWidth();
  343.                     int leftWidth = thumbR.x - leftX;
  344.                     if (leftWidth > 0)
  345.                         paintHorizontalContentBevel(g, leftX, y, leftWidth, h);
  346.  
  347.                     int rightX = thumbR.x + thumbR.width;
  348.                     int rightWidth = incrButton.getX() - rightX;
  349.                     if (rightWidth > 0)
  350.                         paintHorizontalContentBevel(g, rightX, y, rightWidth, h);
  351.                 }
  352.             }
  353.             else
  354.             {
  355.                 g.setColor(scrollBarBackgroundDisabled);
  356.                 g.fillRect(x, y, w, h);
  357.             }
  358.         }
  359.     }
  360.  
  361.     public void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {}
  362.     
  363.     public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
  364.     {        
  365.         if (thumbBounds.isEmpty() || !isActive || !scrollbar.isEnabled() || (scrollbar.getMinimum() >= scrollbar.getMaximum()))
  366.             return;
  367.  
  368.         Icon img = drawScrollBoxPressed ? scrollBoxPressed : scrollBoxEnabled;
  369.         img.paintIcon(c, g, thumbBounds.x, thumbBounds.y);
  370.     }
  371.  
  372.     class ChangeListener implements java.beans.PropertyChangeListener
  373.     {
  374.         public void propertyChange(java.beans.PropertyChangeEvent evt)
  375.         {
  376.             if(evt.getPropertyName().equals("activated"))
  377.             {
  378.                 boolean oldActive = isActive;
  379.                 isActive = ((Boolean) evt.getNewValue()).booleanValue();
  380.                 if(isActive != oldActive)
  381.                 {
  382.                     Object obj = evt.getSource();
  383.                     if(obj instanceof ActivationHelper)
  384.                     {
  385.                         ((ActivationHelper)obj).getComponent().repaint();
  386.                     }
  387.                 }
  388.             }
  389.         }
  390.     }
  391.  
  392.     /* MouseListener functions */
  393.  
  394.     public void mouseEntered(MouseEvent e) {}
  395.  
  396.     public void mouseExited(MouseEvent e) {}
  397.  
  398.     public void mouseClicked(MouseEvent e) {}
  399.  
  400.     public void mousePressed(MouseEvent e)
  401.     {
  402.         if (isDragging)
  403.         {
  404.             drawScrollBoxPressed = true;
  405.             scrollbar.repaint(getThumbBounds());    // Paint the Thumb correctly
  406.         }
  407.     }
  408.     
  409.     public void mouseReleased(MouseEvent e)
  410.     {
  411.         if (drawScrollBoxPressed)
  412.         {
  413.             drawScrollBoxPressed = false;
  414.             scrollbar.repaint(getThumbBounds());    // Paint the Thumb correctly
  415.         }
  416.     }
  417.     
  418.     class ScrollBarButton extends JButton implements SwingConstants
  419.     {
  420.         private int direction = NORTH;
  421.         private Icon scrollArrowDisabled;
  422.         private Icon scrollArrowEnabled;
  423.         private Icon scrollArrowPressed;
  424.     
  425.         public ScrollBarButton(int direction)
  426.         {
  427.             switch (direction) {
  428.             case NORTH:
  429.                 scrollArrowDisabled = UIManager.getIcon("ScrollBar.upArrowDisabled");
  430.                 scrollArrowEnabled = UIManager.getIcon("ScrollBar.upArrowEnabled");
  431.                 scrollArrowPressed = UIManager.getIcon("ScrollBar.upArrowPressed");
  432.                 break;
  433.             case SOUTH:
  434.                 scrollArrowDisabled = UIManager.getIcon("ScrollBar.downArrowDisabled");
  435.                 scrollArrowEnabled = UIManager.getIcon("ScrollBar.downArrowEnabled");
  436.                 scrollArrowPressed = UIManager.getIcon("ScrollBar.downArrowPressed");
  437.                 break;
  438.             case EAST:
  439.                 scrollArrowDisabled = UIManager.getIcon("ScrollBar.rightArrowDisabled");
  440.                 scrollArrowEnabled = UIManager.getIcon("ScrollBar.rightArrowEnabled");
  441.                 scrollArrowPressed = UIManager.getIcon("ScrollBar.rightArrowPressed");
  442.                 break;
  443.             case WEST:
  444.                 scrollArrowDisabled = UIManager.getIcon("ScrollBar.leftArrowDisabled");
  445.                 scrollArrowEnabled = UIManager.getIcon("ScrollBar.leftArrowEnabled");
  446.                 scrollArrowPressed = UIManager.getIcon("ScrollBar.leftArrowPressed");
  447.                 break;
  448.             default:
  449.                 throw new IllegalArgumentException("invalid direction");
  450.             }
  451.             this.direction = direction;
  452.             
  453.             setRequestFocusEnabled(false);
  454.             setOpaque(true);
  455.         }
  456.         
  457.         public Dimension getPreferredSize()
  458.         {
  459.             return new Dimension(16, 16);
  460.         }
  461.         
  462.         public Dimension getMinimumSize()
  463.         {
  464.             return getPreferredSize();
  465.         }
  466.         
  467.         public Dimension getMaximumSize()
  468.         {
  469.             return getPreferredSize();
  470.         }
  471.         
  472.         public boolean isFocusTraversable()
  473.         {
  474.             return false;
  475.         }
  476.         
  477.         public void paint(Graphics g) 
  478.         {
  479.             if (isActive)
  480.             {
  481.                 boolean isPressed = getModel().isPressed();
  482.                 boolean isEnabled = isEnabled();
  483.                 Icon img;
  484.         
  485.                 if (isPressed)
  486.                     img = scrollArrowPressed;
  487.                 else if (isEnabled)
  488.                     img = scrollArrowEnabled;
  489.                 else
  490.                     img = scrollArrowDisabled;
  491.             
  492.                 img.paintIcon(this, g, 0 /*x*/, 0 /*y*/);
  493.             }
  494.         }
  495.     }
  496. }
  497.